package org.wevil.controller.sys;

import org.wevil.entity.sys.SysUser;
import org.wevil.enumu.ResultEnum;
import org.wevil.params.add.UserAP;
import org.wevil.params.update.UserRoleUP;
import org.wevil.repo.sys.SysUserRepo;
import org.wevil.service.sys.UserService;
import org.wevil.util.JwtUtil;
import org.wevil.vo.Result;
import org.wevil.vo.detail.UserDetailVo;
import org.wevil.vo.list.RoleListVo;



import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/** 用户管理 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private SysUserRepo sysUserRepo;

    /** web注册 */
    @PostMapping("/register")
    public Result<String> login(
            @RequestBody @Validated UserAP param
    ) {
        String tel = param.getTel();
        SysUser sysUser = sysUserRepo.findByTel(tel);
        if (sysUser != null) {
            return Result.info(ResultEnum.REPEAT, "手机号重复");
        }
        String username = param.getUsername();
        sysUser = sysUserRepo.findByUsername(username);
        if (sysUser != null) {
            return Result.info(ResultEnum.REPEAT, "用户名重复");
        }
        String email = param.getEmail();
        if(email!=null && email.length()>0){
            sysUser = sysUserRepo.findByEmail(email);
            if (sysUser != null) {
                return Result.info(ResultEnum.REPEAT, "邮箱重复");
            }
        }
        userService.createUser(param);
        return Result.success();
    }

    /** 修改自己的密码 */
    @PutMapping("/changeOwnPwd")
    public Result<String> changeOwnPwd(
            @RequestHeader("token") String token,
            @RequestParam String pwd
    ) {
        Integer userId = JwtUtil.getUserId(token);
        userService.updatePassword(userId, pwd);
        return Result.success();
    }

    /** 修改其他账号密码 */
    @PutMapping("/changePwd")
    public Result<String> changePwd(
            @RequestParam Integer userId,
            @RequestParam String pwd
    ) {
        userService.updatePassword(userId, pwd);
        return Result.success();
    }

    /** 获取用户详细信息 */
    @GetMapping("/detail")
    public Result<UserDetailVo> detail(
            @RequestHeader(value = "token") String token
    ){
        Integer userId = JwtUtil.getUserId(token);
        UserDetailVo data = userService.getUserDetailVo(userId);
        return Result.success(data);
    }

    // -------------------------------------用户关联角色------------------------------------------------//

    /** 获取用户关联的角色 */
    @GetMapping("/userCondRole")
    public Result<List<RoleListVo>> userCondRole(
            @RequestParam Integer userId
    ){
        List<RoleListVo> data = userService.getUserCondRoleList(userId);
        return Result.success(data);
    }

    /** 修改用户关联的角色 */
    @PutMapping("/userCondRole")
    public Result<String> userCondRole(
            @RequestBody UserRoleUP param
    ){
        userService.updateUserCondRoleList(param);
        return Result.success();
    }

}
